package net.lzzy.config;

import net.lzzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity//开启MVC security安全支持
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//    private UserDetailsService userService;
@Autowired
private UserService userService;
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception{
        //密码需要设置编码器
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        //1.使用内存用户信息作为测试
        auth.inMemoryAuthentication().passwordEncoder(encoder)
                .withUser("Heller").password(encoder.encode("123")).roles("admin")
                .and()
                .withUser("Tom").password(encoder.encode("123")).roles("admin");
    }




//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//        auth.userDetailsService(userService).passwordEncoder(encoder);
//    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                //具体规则放前面
                .antMatchers("/back/**").hasRole("admin")
                //注意放行静态资源和前台页面路径
                //.antMatchers( "/assets/**", "/css/**", "/","/article/**").permitAll()
                .antMatchers("/**").permitAll()
                .anyRequest().authenticated();

        http.formLogin()
                .loginPage("/login").permitAll()
                .usernameParameter("username")
                .passwordParameter("password")
                .defaultSuccessUrl("/")
                .failureUrl("/login?error");

        http.logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/");

        http.rememberMe()
                .rememberMeParameter("rememberme")
                .tokenValiditySeconds(200);
    }

//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        http.authorizeRequests()
//                .antMatchers("/back/**").hasRole("admin")
//                .antMatchers("/**").permitAll()
////                .antMatchers("/client/common/**").hasRole("common")
////                .antMatchers("/common/vip/**").hasRole("vip")
//                .anyRequest().authenticated();
////                .and()
////                .formLogin();
//
//        //自定义用户控制
//        http.formLogin()
//                .loginPage("/login").permitAll()
//                .usernameParameter("username").passwordParameter("password")
//                .defaultSuccessUrl("/")
//                .failureUrl("/login?error");
//
//        //自定义用户退出控制
//        http.logout()
//                .logoutUrl("/logout")//指定了用户退出的请求路径，必须与index.html页面退出表单中action的值必须保持一致
//                .logoutSuccessUrl("/");//指定了用户退出成功后重定向到"/"地址
//
//        http.rememberMe()
//                .rememberMeParameter("rememberme")
//                .tokenValiditySeconds(200);
//    }
//
////    @Override
////    protected void configure(AuthenticationManagerBuilder auth) throws
////            Exception {
////        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
////        auth.userDetailsService(userService).passwordEncoder(encoder);
////    }

}